package aishua.com.mytestapplist;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ContentProvider;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.JavascriptInterface;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import aishua.com.mytestapplist.diyiview.MyWebview;
import aishua.com.mytestapplist.utils.LogUtils;
import aishua.com.mytestapplist.webkit.JsMessage;
import aishua.com.mytestapplist.webkit.SharpJS;

/**
 * Created by aishua on 2018/3/22.
 */

public class WebVeiwActivity extends Activity implements View.OnClickListener {
    private static final String TAG = "WebVeiwActivity-";
    //    private WebView webView;
    private MyWebview webView;
    private Button btn_back;
    private Button btn_refresh;
    private Button btn_top;
    private TextView tv_newsTile;
    private LinearLayout ll_title;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);

        initView();

        final WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webView.setHorizontalScrollBarEnabled(true);
        webView.setVerticalScrollBarEnabled(true);
        webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
//        webSettings.setBuiltInZoomControls(true);
//        webSettings.setUseWideViewPort(true);
//        webSettings.setSupportZoom(true);
        webView.loadUrl("file:///android_asset/contacts.html");
//        webView.addJavascriptInterface(new JsMessage(this),"jsmessage");
        SharpJS sharpJS = new SharpJS(this, webView);
        webView.addJavascriptInterface(sharpJS, "sharp");
        String contacts = sharpJS.buildJson(sharpJS.getContacts());
        LogUtils.v(TAG + "contacts:" + contacts);
//        webView.loadUrl("javascript:show('" + contacts + "')");
        System.out.print("Thread.name:" + Thread.currentThread().getName());
//        webView.loadUrl("https://www.baidu.com");
//         btn_loadUrl.setOnClickListener(new View.OnClickListener() {
//             @Override
//             public void onClick(View v) {
//
//                webView.post(new Runnable() {
//                    @Override
//                    public void run() {
//
//                        webView.loadUrl("javascript:callJS()");
//                    }
//                });
//             }
//         });
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                LogUtils.v(TAG + "url:" + url);
//              webView.loadUrl(url);
//                return true;
                return false;
//                return super.shouldOverrideUrlLoading(view,url);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                CookieManager cookieManager = CookieManager.getInstance();
                String cookContent = cookieManager.getCookie(url);
                LogUtils.v(TAG + "cookContent:" + cookContent);
            }
        });
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
                LogUtils.v(TAG + "message:" + message);
//                AlertDialog.Builder b = new AlertDialog.Builder(WebVeiwActivity.this);
//                b.setTitle("Alert");
//                b.setMessage(message);
//                b.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//                        result.confirm();
//                    }
//                });
//                b.setCancelable(false);
//                b.create().show();
                return true;
            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                tv_newsTile.setText(title);
            }
        });
        webView.setScrollChangedListener(new MyWebview.ScrollChangedListener() {
            @Override
            public void onScroll(int l, int t, int oldl, int oldt) {
                LogUtils.v(TAG + "l：" + l + ",t:" + t + ",oldl:" + oldl + ",oldt:" + oldt);
            }
        });
    }

    private void initView() {
        webView = (MyWebview) findViewById(R.id.webView);
        btn_refresh = (Button) findViewById(R.id.btn_refresh);
        btn_top = (Button) findViewById(R.id.btn_top);
        tv_newsTile = (TextView) findViewById(R.id.tv_newTitle);
        btn_back = (Button) findViewById(R.id.btn_back);
        ll_title = (LinearLayout) findViewById(R.id.ll_title);
        btn_back.setOnClickListener(this);
        btn_refresh.setOnClickListener(this);
        btn_top.setOnClickListener(this);
        ll_title.setVisibility(View.GONE);

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_refresh:
                webView.reload();
                break;
            case R.id.btn_top:
                webView.setScrollY(0);
                break;
        }

    }

    long exitTime = 0;

    @Override
    public void onBackPressed() {
//        super.onBackPressed();
        if (webView.canGoBack()) {
            webView.goBack();
        } else {

            if (System.currentTimeMillis() - exitTime > 2000) {
                Toast.makeText(this, "再次点击退出程序", Toast.LENGTH_LONG).show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                Toast.makeText(this, "退出应用", Toast.LENGTH_LONG).show();

            }
        }

    }


}
